package com.itheima.web.controller.sms;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.sms.IFlashPromotionProductRelationService;
import com.itheima.utils.PageResultList;
import com.itheima.utils.ResultData;
import com.itheima.vo.sms.FlashPromotionProductRelation;
import com.itheima.vo.sms.FlashPromotionProductVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品限时购与商品关系表 前端控制器
 */
@RestController
@RequestMapping("/flashProductRelation")
public class FlashPromotionProductRelationController {
    @DubboReference
    private IFlashPromotionProductRelationService flashPromotionProductRelationService;

    @PostMapping("/create")
    public ResultData save(@RequestBody List<FlashPromotionProductRelation> flashPromotionProductRelation) {
        flashPromotionProductRelationService.saveBatch(flashPromotionProductRelation);
        return ResultData.ok("", "");
    }

    @PostMapping("/delete/{id}")
    public ResultData delete(@PathVariable Long id) {
        flashPromotionProductRelationService.removeById(id);
        return ResultData.ok("删除成功", "");
    }

    @GetMapping("/list")
    public ResultData list(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        Page<FlashPromotionProductVo> list = flashPromotionProductRelationService.getList(flashPromotionId, flashPromotionSessionId, pageNum, pageSize);
        PageResultList pageResult = new PageResultList();
        pageResult.setList(list.getRecords());
        pageResult.setPageSize(list.getSize());
        pageResult.setTotalPage(list.maxLimit());
        pageResult.setTotal(list.getTotal());
        pageResult.setPageNum(list.getPages());
        return ResultData.ok("", pageResult);
    }

    @PostMapping("/update/{id}")
    public ResultData update(@PathVariable("id") Long id, @RequestBody FlashPromotionProductRelation flashPromotionProductRelatio) {
        flashPromotionProductRelatio.setId(id);
        flashPromotionProductRelationService.updateById(flashPromotionProductRelatio);
        return ResultData.ok("", "");
    }

    @GetMapping("{id}")
    public ResultData getById(@PathVariable("id") Long id) {
        FlashPromotionProductRelation byId = flashPromotionProductRelationService.getById(id);
        return ResultData.ok("", byId);
    }
}

